package mvp.levelup.springmvc.demo.controller;

import mvp.levelup.springmvc.demo.domain.SomeModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Valera on 14.09.2014.
 */
@Controller
public class DemoController {
    @RequestMapping(value = "hello", method = RequestMethod.GET)
    @ResponseBody
    public String sayHello() {
        return "Hello!";
    }

    @RequestMapping(value = "helloparam", method = RequestMethod.GET)
    @ResponseBody
    public String sayWithParam(@RequestParam("param") String data) {
        return "It says " + data;
    }

    @RequestMapping(value = "model", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public SomeModel getSomeModel() {
        return new SomeModel(1, "Test", new Date());
    }

    @RequestMapping(value = "/{data}/", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Map showData(@PathVariable String data, @RequestHeader("Host") String host) {
        Map<String, String> toSend = new HashMap<String, String>();
        toSend.put("pathData", data);
        toSend.put("host", host);
        return toSend;
    }

    @RequestMapping(value = "model", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public ResponseEntity<String> saveSomeModel(@RequestBody SomeModel someModel) {
        if (someModel != null) {
            return new ResponseEntity<String>("ok", HttpStatus.OK);
        } else {
            return new ResponseEntity<String>("not ok", HttpStatus.BAD_REQUEST);
        }
    }
}
